class RoleWarehouse < ActiveRecord::Base
  belongs_to :company
  belongs_to :role
  belongs_to :employee
  belongs_to :warehouse

  #根据角色得到权限内仓库
  scope :role_warehouse, lambda { |company_id, role_id|
    where('company_id = ? AND role_id = ?', company_id, role_id).includes([:warehouse])
  }

  scope :role_warehouse_unlocked, lambda { |company_id, role_id|
    where('company_id = ? AND role_id = ? AND is_locked = 0', company_id, role_id).includes([:warehouse])
  }

  #输出array
  def self.getRoleWarehouseUnlockedArray company_id, role_id
    roleWarehouses = RoleWarehouse.where('role_id = ? AND is_locked = 0', role_id).includes([:warehouse])

    warehouseIds = Array.new
    roleWarehouses.each do |roleWarehouse|
      warehouseIds << roleWarehouse.warehouse.id
    end
    warehouseIds
  end

end
